package com.tkjt.pms.modules.scoring.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tkjt.pms.modules.scoring.model.ScoreCommentSaveRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreValueSaveRequestModel;
import com.tkjt.pms.modules.scoring.service.TkScoringService;

@RestController
@RequestMapping("/api/pms/scoring")
public class TkScoringController {
    @Autowired
    private TkScoringService tkScoringService;


    /**
     * 据【用户角色】获得可以进行评分的指标信息：
     * 1)一线单位账号，获取：重点工作
     * 2)养康本部账号，获取：质量改进、重点工作
     */
    @GetMapping("indexCodes")
    public Result<?> indexCodes(){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return tkScoringService.indexCodes(loginUser);

    }

    /**
     * *获取双向评分列表信息
     */
    @PostMapping("scoreDatagrid")
    public Result<?> scoreDatagrid(@RequestBody ScoreDatagridRequestModel model) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return tkScoringService.scoreDatagrid(loginUser,model);
    }


    /**
     * 双向-列表保存-分数保存
     * 保存绩效填报信息
     */
    @PostMapping(value = "scoreValueSave")
    public Result<?> saveRows(@RequestBody ScoreValueSaveRequestModel model){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return tkScoringService.saveRows(loginUser,model);
    }

    /**
     * 双向-列表保存-意见保存
     * 保存绩效填报信息
     */
    @PostMapping("scoreCommentSave")
    public Result<?> scoreCommentSave(@RequestBody ScoreCommentSaveRequestModel model){
        try {
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if(loginUser==null) {
                return Result.error("用户非法！");
            }
            return tkScoringService.scoreCommentSave(loginUser, model);
        } catch (Exception e) {
            if("indexError".equals(e.getMessage())){
                return Result.error(e.getCause().getMessage());
            }else{
                return Result.error("未知错误");
            }
        }
    }

    /**
     * *双向-评分意见-单条查询 (已删除接口)
     * *该接口用于一线单位或养康本部角色的用户账号
     * *获取已存在的评分意见
     */
//    @PostMapping("scoreCommentQuery")
//    public Result scoreCommentQuery(@RequestBody ScoreCommentQueryRequestModel model){
//        try {
//            return Result.ok(tkScoringService.scoreCommentQuery(model));
//        } catch (Exception e) {
//            if("indexError".equals(e.getMessage())){
//                return Result.error(e.getCause().getMessage());
//            }else{
//                return Result.error("未知错误");
//            }
//        }
//    }

    /**
     * *我的绩效-绩效概览接口
     * *获取绩效历史得分列表
     */
    @PostMapping("gradeViewDatagrid")
    public Result<?> gradeViewDatagrid(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return Result.ok(tkScoringService.gradeViewDatagrid(loginUser,request));
    }

    @PostMapping("gradeDetailDatagrid")
    public Result<?> gradeDetailDatagrid(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return Result.ok(tkScoringService.gradeDetailDatagrid(loginUser,request));
    }

    /**
     * *我的绩效-指标图表(达成率)
     */
    @RequestMapping("gradeIndexChart")
    public Result<?> gradeIndexChart(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return tkScoringService.gradeIndexChart(loginUser,request);
    }

    /**
     * *我的绩效-指标图表(达成率)(新接口)
     */
    @RequestMapping("gradeIndexChartNew")
    public Result<?> gradeIndexChartNew(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return tkScoringService.gradeIndexChartNew(loginUser,request);
    }
    
    /**
     * *我的绩效-指标图表(达成率)(新接口)
     */
    @RequestMapping("gradeEvents")
    public Result<?> gradeEvents(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser==null) {
            return Result.error("用户非法！");
        }
        return tkScoringService.gradeEvents(loginUser);
    }
    
}
